package cn.otrue.patienthealthmanager.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.activity.DoctorParticularsActivity;
import cn.otrue.patienthealthmanager.adapter.FindDoctorAllFragmentAdpater;
import cn.otrue.patienthealthmanager.bean.DoctorBaseinfo;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.CommonUtil;
import cn.otrue.patienthealthmanager.utils.MyConst;
import cn.otrue.patienthealthmanager.view.XListView;
import cn.otrue.patienthealthmanager.view.XListView.IXListViewListener;

import com.google.gson.reflect.TypeToken;

/**
 * 找医生->关注医生
 * 
 * @author G
 * 
 */
public class FindDoctorAttentionFragment extends BaseFragment implements
		OnTouchListener, IXListViewListener {

	private List<DoctorBaseinfo> allDoctor = new ArrayList<DoctorBaseinfo>();
	private XListView lv_attention_list;
	private View rootView;

	private FindDoctorAllFragmentAdpater adapter;

	private int currentPage = 1;

	private Handler handler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			onLoad();
			if (state.equals("001")) {
				String dataStr = data.getString("data");
				List<DoctorBaseinfo> newList = HttpServer.gson.fromJson(
						dataStr, new TypeToken<List<DoctorBaseinfo>>() {
						}.getType());
				int uuu = allDoctor.size();

				CommonUtil.listRemoveNull(allDoctor);
				allDoctor.addAll(newList);
				if (allDoctor.size() > 5) {
					lv_attention_list.setPullLoadEnable(true);

				}
				adapter.notifyDataSetChanged();
				lv_attention_list.setSelection(uuu);

			} else {
				onLoad();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			onLoad();
		}

	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		rootView = inflater.inflate(R.layout.fragment_doctor_attention,
				container, false);

		initView(rootView);
		return rootView;
	}

	private void initView(final View rootView) {

		lv_attention_list = (XListView) rootView
				.findViewById(R.id.lv_attention_list);
		lv_attention_list.setPullLoadEnable(false); // 是否开启上拉加载
		lv_attention_list.setXListViewListener(this);// 上啦加载和下拉刷新的监听
		lv_attention_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent();

				intent.putExtra("aliasAccount", allDoctor.get(arg2 - 1)
						.getAliasAccount());
				intent.putExtra("jid", allDoctor.get(arg2 - 1).getJid());
				intent.putExtra("isAttention", allDoctor.get(arg2 - 1)
						.getIsAttention());
				intent.setClass(rootView.getContext(),
						DoctorParticularsActivity.class);

				startActivity(intent);

			}
		});
		allDoctor.clear();
		adapter = new FindDoctorAllFragmentAdpater(rootView.getContext(),
				allDoctor, MyConst.ATTENTION_DOCTOR);
		lv_attention_list.setAdapter(adapter);

		isInitViewCompleted = true;
	}

	private void onLoad() {
		lv_attention_list.stopRefresh();
		lv_attention_list.stopLoadMore();
		lv_attention_list.setRefreshTime("刚刚");
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		lv_attention_list.setPullLoadEnable(false);
		super.onResume();
		allDoctor.clear();
		getData(currentPage);

	}

	private void getData(int i) {

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("sender", MyConst.getACCOUNT());
		map.put("isAttention", 1);
		TransferParameter parameter = new TransferParameter();
		parameter.setPagesize("20");
		parameter.setCurrentPage(i + "");
		parameter.setData(map);
		new HttpServer()
				.post(HttpCons.QUERY_ATTENTION_INFO, parameter, handler);

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	public static Fragment newInstance() {
		return new FindDoctorAttentionFragment();
	}

	@Override
	public String setPageName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		lv_attention_list.setPullLoadEnable(false);
		if (!TextUtils.isEmpty(MyConst.getACCOUNT())) {
			allDoctor.clear();
			currentPage = 1;
			getData(currentPage);
		} else {
			allDoctor.clear();
			adapter.notifyDataSetChanged();
			onLoad();
		}
	}

	@Override
	public void onLoadMore() {
		lv_attention_list.setPullLoadEnable(false);
		// TODO Auto-generated method stub
		if (!TextUtils.isEmpty(MyConst.getACCOUNT())) {
			currentPage++;
			getData(currentPage);
		} else {
			allDoctor.clear();
			adapter.notifyDataSetChanged();
			onLoad();
		}
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (isVisibleToUser) {
			lv_attention_list.setPullLoadEnable(false);
			// 相当于Fragment的onResume
			if (!TextUtils.isEmpty(MyConst.getACCOUNT())) {
				if (isInitViewCompleted) {
					if (FindDoctorAllFragment.isRefresh == 1) {
						allDoctor.clear();
						adapter.notifyDataSetChanged();
						currentPage = 1;

						getData(currentPage);
						FindDoctorAllFragment.isRefresh = 0;
					}
				}

			} else {
				allDoctor.clear();
				adapter.notifyDataSetChanged();
			}

		} else {
			// 相当于Fragment的onPause

		}
	}
}
